#!/usr/bin/env python

from pgap import Gene 
import random

class IntegerGene(Gene.Gene):
	allele 	   = 0 
	lowerBound = 0 
	upperBound = 1
	range	   = None	
	mutAmt	   = 0.1

	def clone(self):
		cloneGene = IntegerGene()
		cloneGene.allele = self.allele
		cloneGene.lowerBound = self.lowerBound
		cloneGene.upperBound = self.upperBound
		cloneGene.range	= self.range
		return cloneGene	

	def setBounds(self,lowerBound,upperBound):
		self.upperBound = upperBound
		self.lowerBound = lowerBound
		if (self.upperBound < self.lowerBound):
			raise "Bound error in IntegerGene"
		self.range = self.upperBound - self.lowerBound

	def setRandomValue(self):
		self.allele = self.lowerBound + \
			random.random()*(self.upperBound - self.lowerBound)

	def getValue(self):
		return int(self.allele)

	def mutate(self):
		if random.random() < 0.5:
	        	self.allele -= int(random.uniform(0, self.mutAmt * \
						(self.allele-self.lowerBound)))
        	else:
            		self.allele += int(random.uniform(0, self.mutAmt * \
					(self.upperBound -self.allele)))
	

	def __str__(self):
		str = "IntegerGene(%d)" %self.getValue()
		return str 

	def __repr__(self):
		str = "IntegerGene(%d)" %self.getValue()
		return str 
